//
//  DSUtils.m
//  DSDeveloperKit
//
//  Created by FengDongsheng on 12/8/6.
//  Copyright (c) 2012年 FengDongsheng. All rights reserved.
//

#import "DSUtils.h"
#import "Keychain.h"

@implementation DSUtils

+ (NSString *)generateRandomStringWithLength:(NSInteger)iLength{
    char data[iLength];
    for (int x = 0; x < iLength; x++) {
        int j = '0' + (arc4random_uniform(75));
        if((j >= 58 && j <= 64) || (j >= 91 && j <= 96)){
            --x;
        }else{
            data[x] = (char)j;
        }
    }
    NSString *text = [[NSString alloc] initWithBytes:data length:iLength encoding:NSUTF8StringEncoding];
    return text;
}

+ (NSString*)getUUIDString{
    NSString * currentDeviceUUIDStr = [XJXKeychain passwordForService:@"com.trump.CarLoan"account:@"uuid"];
    if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
    {
        NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
        currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
        [XJXKeychain setPassword: currentDeviceUUIDStr forService:@"com.trump.CarLoan"account:@"uuid"];
    }
    return currentDeviceUUIDStr;
}

+ (UIViewController *)obtainTopViewController{
    return [DSUtils obtainTopViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

+ (UIViewController*)obtainTopViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [DSUtils obtainTopViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [DSUtils obtainTopViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [DSUtils obtainTopViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

#pragma mark ------------  判断数组对象是否为空
+ (BOOL)isNotNullObject:(NSArray *)array {
    if (array == nil || array == [NSNull class] || [array isKindOfClass:[NSNull class]] || [array isEqual:@"null"] || [array isEqual:@"<nil>"] || [array isEqual:@"(null)"] || [array isEqual:@"NULL"] || [array isEqual:@""] || array.count == 0) {
        return NO;
    }else {
        return YES;
    }
}

#pragma mark --------  判断字符串为空的各种情况
+ (BOOL)isBlankString:(NSString *)string {
    if (string == nil || string == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return NO;
    } else {
        return YES;
    }
}

@end
